//
//  LabelViewController.swift
//  StealPicture
//
//  Created by ?! 郭斌勇 on 16/8/12.
//  Copyright © 2016年 ?! 郭斌勇. All rights reserved.
//

import UIKit

let 标签tableHeight:CGFloat = 300.0
let 标签tableWidth:CGFloat = 150.0




class LabelViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate {
    @IBOutlet weak var 标签table: UITableView!

    var 触发cell:UploadImageTableViewCell!
    
    var 本地挂载中心 = CGPoint(x: 0, y: 0)
    var 被挂载视图size = CGSize(width: 0, height: 0)
    var 挂载点在上 = true
    
    var keyWindoew挂载矩形:CGRect {
        get{
            if 挂载点在上{
                return CGRect(origin: CGPoint(x: 本地挂载中心.x - 被挂载视图size.width/2, y: 本地挂载中心.y - 被挂载视图size.height), size: 被挂载视图size)
            }else{
                return CGRect(origin: CGPoint(x: 本地挂载中心.x - 被挂载视图size.width/2, y: 本地挂载中心.y), size: 被挂载视图size)
            }
            
        }
        
        set{
            
            let 本地挂载rect = view.convertRect(newValue, fromView: UIApplication.sharedApplication().keyWindow)
            
            被挂载视图size = 本地挂载rect.size
            挂载点在上 = (  本地挂载rect.maxY + 标签tableHeight < view.frame.maxY)
            
            if 挂载点在上 {
                本地挂载中心 = CGPoint(x: 本地挂载rect.midX, y: 本地挂载rect.maxY)
            }else{
                本地挂载中心 = CGPoint(x: 本地挂载rect.midX, y: 本地挂载rect.minY)
            }
            
        }
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()


        
    }
    

    override func viewDidAppear(animated: Bool) {
        标签table动画_函数()
    }
    
    
    

    private func 标签table动画_函数(){
        
        
        
        // Do any additional setup after loading the view.
        
        if 挂载点在上 {
            标签table.frame = CGRect(x: 本地挂载中心.x -  标签tableWidth/2, y: 本地挂载中心.y, width: 标签tableWidth, height: 0)

        }else{
            
            标签table.frame = CGRect(x: 本地挂载中心.x - 标签tableWidth/2, y: 本地挂载中心.y, width: 标签tableWidth, height: 0)
            
        }
        
       
        
        
        UIView.animateWithDuration(0.5, animations: {
            
            if self.挂载点在上 {
                self.标签table.frame.size.height = 标签tableHeight
            }else{
                
                let 标签y点 = self.本地挂载中心.y - 标签tableHeight
                
                if 标签y点 > 0 {
                    self.标签table.frame.origin.y = 标签y点
                    self.标签table.frame.size.height = 标签tableHeight
                }else{
                    
                    self.标签table.frame.origin.y = 0
                    self.标签table.frame.size.height = self.本地挂载中心.y
                    
                }
                
                

            }
            
            
            
                    })
        

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
       
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    //MARK:代理方法

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 内容类别集合_全局量.内容类别数目
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("LabelViewControllerCell", forIndexPath: indexPath)
        cell.textLabel?.text = 内容类别集合_全局量.根据序号返回相应的类别(indexPath.row)?.名称
        return cell
    }
    
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "请选择标签"
        
    }
    
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        
        触发cell.图片信息?.内容类别 = 内容类别集合_全局量.根据序号返回相应的类别(indexPath.row)
        self.performSegueWithIdentifier("LabelViewController_返回", sender: "标签选择")
    }
    
    
    
    
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool{
        return touch.view == view
    }
    
    
    
    
    
    
}
